import SwiftUI
import Combine

/// 跨平台焦点管理器
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
class TVFocusManager: ObservableObject {
    @Published var shouldFocusNavigation: Bool = false
    @Published var currentFocusArea: FocusArea = .navigation
    @Published var targetNavigationIndex: Int = 0
    @Published var currentPageIndex: Int = 0
    
    private var cancellables = Set<AnyCancellable>()
    
    enum FocusArea {
        case navigation
        case content
    }
    
    init() {
        
    }
    
    func requestNavigationFocus(targetIndex: Int = 0) {
        print("TVFocusManager: 请求导航焦点，目标索引: \(targetIndex)")
        DispatchQueue.main.async {
            self.targetNavigationIndex = targetIndex
            self.shouldFocusNavigation = true
            self.currentFocusArea = .navigation
        }
    }
    
    func requestContentFocus() {
        print("TVFocusManager: 请求内容焦点，当前页面索引: \(self.currentPageIndex)")
        DispatchQueue.main.async {
            self.shouldFocusNavigation = false
            self.currentFocusArea = .content
        }
    }
    
    func setCurrentPageIndex(_ index: Int) {
        print("TVFocusManager: 设置当前页面索引: \(index)")
        DispatchQueue.main.async {
            self.currentPageIndex = index
        }
    }
    
    func reset() {
        DispatchQueue.main.async {
            self.shouldFocusNavigation = false
            // 不要改变 currentFocusArea，保持当前状态
        }
    }
}
